package com.feeyo.groundservice.vehiclemonitorserver;

import com.feeyo.groundservice.base.rdb.PostgresDb;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by fooevr on 2016/12/29.
 */
public class VehicleInfo extends AbstractVerticle {
	Logger logger = LoggerFactory.getLogger(VehicleInfo.class);
    public static Map<String, Vehicle> vehicleInfos = new HashMap<>();
    @Override
    public void start(Future<Void> startFuture) throws Exception {
    	try{
        PostgresDb.postgreSQLClient.getConnection(conn->{
            if(conn.failed()){
                System.out.println(conn.cause());
                return;
            }
            if(conn.succeeded()){
	            conn.result().query("select id, devid, department, vehicletype, posowner,number from vehiclemonitor.vehicle where not _deleted", r->{
	                conn.result().close();
	                if(r.failed()){
	                    System.out.println(r.cause());
	                    return;
	                }
	                vehicleInfos.clear();
	                for(int i = 0; i < r.result().getNumRows(); i++){
	                    JsonObject row = r.result().getRows().get(i);
	                    Vehicle vehicle = new Vehicle();
	                    vehicle.setId(row.getString("id")) ;
	                    vehicle.setDevid(row.getString("devid"));
	                    vehicle.setDepartmentId(row.getString("department"));
	                    vehicle.setTypeId(row.getString("vehicletype"));
	                    vehicle.setPosowner(row.getString("posowner"));
	                    vehicle.setNumber(row.getString("number"));
	                    vehicleInfos.put(vehicle.getDevid().toUpperCase(), vehicle);
	                    //System.out.println("airport->"+vehicle.getPosowner() + "-"+vehicle.getDevid());
	                }
	                startFuture.complete();
	                logger.info("load vehicleinfos " + vehicleInfos.size());
	            });
            }
        });
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }


}
